<?php 
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: ?'); }

if (isset($url['o']) && !empty($url['o'])) { 
	switch ($url['o']) {
		case "Veiksmas": {
			$order = "action";
			break;
		}
		case "Nr:": {
			$order = "id";
			break;
		}
		case "Nickas": {
			$order = "nick";
			break;
		}
		case "IP": {
			$order = "ip";
			break;
		}
		default: {
			$order = "id";
			break;
		}
	}
}
else { $order = "id"; }

//trinam irasa
if (isset($url['d']) && isnum($url['d']) && LEVEL == 30) {
	if ($url['d'] == "0" && isset($_POST['ip']) && !empty($_POST['ip']) && $_POST['del_all'] == 'Valyti' && isnum($_POST['ip'])) {
			$sql = mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."logai` WHERE `ip` = ".escape($_POST['ip']));
			msg("Išvalyta","Ištryniau visus <b>".long2ip($_POST['ip'])."</b> įrašus"); redirect("?id,46;a,19","meta");
	}
	elseif (!empty($url['d'])) {
		mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."logai` WHERE `id` = ".escape($url['d'])." LIMIT 1;");
		header("location: ?id,46;a,19");
	}
}
elseif (isset($url['v']) && !empty($url['v']) && isnum($url['v'])) {
	$sql = mysql_fetch_assoc(mysql_query1("SELECT id, INET_NTOA(ip) AS ip, action, time FROM `".LENTELES_PRIESAGA."logai` WHERE id=".escape($url['v'])." LIMIT 1"));
	lentele($sql['ip']." - ".kada($sql['time']),input($sql['action']));
}
else {
		include_once("priedai/class.php");
		//$sql = mysql_query1("SELECT id, INET_NTOA(ip) AS ip, action, time FROM `logai` ORDER BY $order DESC LIMIT 0 , 100 ");
		$sql = mysql_query1("
		SELECT `".LENTELES_PRIESAGA."logai`.`id`, `".LENTELES_PRIESAGA."logai`.`ip`, `".LENTELES_PRIESAGA."logai`.`action`, INET_NTOA(`".LENTELES_PRIESAGA."logai`.`ip`) AS ip1, `".LENTELES_PRIESAGA."logai`.`time`,
		IF(`".LENTELES_PRIESAGA."users`.`nick` <> '', `".LENTELES_PRIESAGA."users`.`nick`, 'Svecias') AS nick,
		IF(`".LENTELES_PRIESAGA."users`.`id` <> '', `".LENTELES_PRIESAGA."users`.`id`, '0') AS nick_id
		FROM `".LENTELES_PRIESAGA."logai` Left Join `".LENTELES_PRIESAGA."users` ON `".LENTELES_PRIESAGA."logai`.`ip` = `".LENTELES_PRIESAGA."users`.`ip`
		ORDER BY $order DESC LIMIT 0 , 100
		");
		$viso = kiek('logai');
		if (mysql_num_rows($sql) > 0) {
			$info = array();
			while($row = mysql_fetch_assoc($sql)) {
				$info[] = array(
					//"Nr:"=>$row['id'],
					"Veiksmas"=> "<a href=\"".url("v,".$row['id']."")."\" title=\"header=[Nr:".$row['id']."] body=[
						<br/>
						Laikas: <b>".$row['time']."</b><br/>
						Kada: <b>".kada($row['time'])."</b><br/>
						IP: <b>".$row['ip1']."</b><hr/>
						Klaida: <i>".wrap1(input($row['action']),50)."</i><br/>
						] fade=[on]\">".trimlink(input(strip_tags($row['action'])),50)."</a>",
				//"Veiksmas"=>trimlink(input($row['action']),50),
				//"IP"=>$row['ip1'],
				"Nickas"=>"<a href='http://www.mrcbug.com/?id,47;m,".$row['nick_id']."'>".$row['nick']."</a> <a href=\"?id,45;n,1;u,".input($row['nick'])."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\"/></a>",
				//"Kada"=>kada($row['time']),
				"Valdymas"=>"<a href=\"".url("d,".$row['id']."")."\" title='Ištrinti įrašą'><img src=\"images/icons/cross.png\" alt=\"[b]\" border=\"0\" class=\"middle\" /></a> <a href='?id,46;a,6;b,".$row['ip']."' title='Uždrausti vartotoją'><img src=\"images/icons/delete.png\" alt=\"[d]\" border=\"0\" class=\"middle\" /></a>"
				);
			}
			$bla = new Table();
			lentele("Svetainės Klaidų Registras - Tavo IP: <font color='red'>".getip()."</font>",$bla->render($info),false,array('Viso',$viso));
		} else { msg("Svetainės LOGAI","Šiuo metu neužfiksuota jokių klaidų"); }
		?>
		</p>
		<p>
		<?
		$sql = mysql_query1("SELECT count(*) as viso, ip, INET_NTOA(ip) AS ip1 FROM `".LENTELES_PRIESAGA."logai` GROUP BY ip ORDER BY time DESC");
		if (mysql_num_rows($sql) > 0) {
			while($row = mysql_fetch_assoc($sql)) {
				$select[$row['ip']] = $row['ip1']." - ".$row['viso'];
			}
			$nustatymai = array(
				"Form"=>array("action"=>url('d,0'),"method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"reg"),
				"Trinti pagal IP:"=>array("type"=>"select","value"=>$select,"selected"=>ip2long($_SERVER['REMOTE_ADDR']),"name"=>"ip"),
				""=>array("type"=>"submit","name"=>"del_all","value"=>"Valyti")
				);
			$bla = new forma();
			lentele("Automatinis trinimas/valymas",$bla->form($nustatymai));
		}
		?>
		</p>
	</div>
</div>
<? } ?>